﻿using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using GSSoftSL.ViewModel;
using GSSoftSL.Web;
using GSSoftSL.Web.Models;
using System.Linq;

namespace GSSoftSL.Views.NghiepVu
{
    /// <summary>
    /// Description for DiemSoMainView.
    /// </summary>
    public partial class DiemSoMainView : UserControl
    {
        private DiemSoCotDiemView _child;

        /// <summary>
        /// Initializes a new instance of the DiemSoMainView class.
        /// </summary>
        public DiemSoMainView()
        {
            InitializeComponent();
            DiemSoViewModel vm = this.DataContext as DiemSoViewModel;
            vm.SetGridColumnPropertyCallBack = SetColumnProperty;
        }

        private void btnAddCotDiem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            DiemSoViewModel vm = this.DataContext as DiemSoViewModel;
            if (vm.CotDiems.Count > 30)
            {
                MessageWindow.CreateNew("Thao tác không hợp lệ", "Mỗi khóa học chỉ có tối đa 30 cột điểm!");
                return;
            }
            if (_child == null)
                _child = new DiemSoCotDiemView();
            vm.CotDiemInfo = new CotDiemInfo();
            _child.SetMode(EnumMode.AddNew);
            _child.Show();
        }

        private void btnEditCotDiem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_child == null)
                _child = new DiemSoCotDiemView();
            DiemSoViewModel vm = this.DataContext as DiemSoViewModel;
            if (vm.CotDiemInfo == null)
            {
                //MessageWindow.CreateNew("Bạn vui lòng chọn 1 ngày điểm danh trước.");
                return;
            }
            _child.SetMode(EnumMode.Modified);
            _child.Show();
        }

        private void btnRemoveCotDiem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            
        }

        private void SetColumnProperty()
        {
            DiemSoViewModel vm = this.DataContext as DiemSoViewModel;
            PropertyInfo info;
            if (vm != null && vm.CotDiems != null)
            {
                var query = vm.CotDiems.Where(n => n.IsSelected);
                for (int i = 2; i < gridDiem.Columns.Count; i++)
                    gridDiem.Columns[i].Visibility = Visibility.Collapsed;
                foreach (CotDiemInfo item in query)
                {
                    gridDiem.Columns[item.STT + 1].Visibility = Visibility.Visible;
                    gridDiem.Columns[item.STT + 1].Header = item.TenCot;
                }
            }
            else
            {
                for (int i = 2; i < gridDiem.Columns.Count; i++)
                    gridDiem.Columns[i].Visibility = Visibility.Collapsed;
            }
        }

        private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	MessageWindow.CreateNew("123");
        }

        private void gridDiem_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }

        private void gridDiem_CurrentCellChanged(object sender, System.EventArgs e)
        {
            if (gridDiem.CurrentColumn != null && gridDiem.CurrentColumn.DisplayIndex >= 2)
            {
                gridDiem.BeginEdit();
                TextBox txt = gridDiem.Columns[gridDiem.CurrentColumn.DisplayIndex].GetCellContent(gridDiem.SelectedItem) as TextBox;
                if (txt != null)
                {
                    txt.Focus();
                    txt.SelectAll();
                    //txt.SelectionStart = 0;
                    txt.Focus();
                }
            }
        }
    }
}